/**
 * axios 网络请求封装
 */
import axios from 'axios'
axios.defaults.withCredentials = false


import qs from 'qs'//qs是一个url参数转化（parse和stringify）的js库
import Vue from 'vue'
import store from '../store'
import Router from '../router'
import url from './http_conf'
import {
    Dialog
} from 'vant';

const port = '';
const http_url = url + (port == '' ? '' : (':' + port)) + '/'
var httpUrl = ''
if (process.env.NODE_ENV === 'development') {
    // httpUrl = '/api'
    httpUrl = http_url
} else if (process.env.NODE_ENV === 'debug') {
    httpUrl = http_url
} else if (process.env.NODE_ENV === 'production') {
    httpUrl = http_url
}


const instance = axios.create({
    baseURL: httpUrl,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    }
});


const isFormData = (v) => {
    return Object.prototype.toString.call(v) === '[object FormData]';
}

/**
 *axios 全局 拦截器
 */

instance.interceptors.response.use(function (response) {

    //忽略返回类型, 取消拦截判断
    // const ignoreContentTypeList = ['image/gif', 'application/vnd.ms-excel;charset=utf-8'];
    // if (ignoreContentTypeList.indexOf(response.headers['content-type']) > -1) return response.data;

    //这里统一处理服务器code
    const Code = response.data.code;
    console.log('code', Code);
    if (Code == 1) {
        return response.data
    } else {
        if (Code == 2) {
            Dialog.alert({
                title: '提示',
                message: '登录状态已过期，请重新登录',
            }).then(() => {
                store.commit("userInfo", "")
                store.commit("sessionId", "")
                setTimeout(()=>{
                    Router.push("/members")
                },100)
            });
        } else {
            if (Code == 101) {
                localStorage.token = '';
            } else {
                return response.data;
            }
        }
    }
}, function (error) {
    if (sessionStorage.locale == "en") {
    } else {
    }
    return Promise.reject(error);
});

/**
 * Request
 * @param {String} url
 * @param {Object} options request参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const request = (url, options = {}, tokenFlag = true) => {
    options = {
        url,
        ...options
    }
    return instance.request(options);
}

/**
 * POST
 * @param {String} url
 * @param {Object} data data参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const POST = (url, data = {}, tokenFlag = true) => {
    console.log('post data', data)
    if (store.state.sessionId && tokenFlag) {
        data = Object.assign(data, {
            sessionId: store.state.sessionId
        })
    }
    // data = Object.assign(data, {
    //     // sessionId: store.state.sessionId
    //     sessionId:'1096267d3446489b9faf16225510bc00'
    // })

    if (!isFormData(data)) data = qs.stringify(data);
    return instance.post(url, data);
}

/**
 * GET
 * @param {String} url
 * @param {Object} data data参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const GET = (url, options = {}, tokenFlag = true) => {
    return instance.get(url, options);
}


/**
 * PUT
 * @param {String} url
 * @param {Object} data data参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const PUT = (url, params = {}, tokenFlag = true) => {
    return instance.put(url, params);
}


/**
 * DELETE
 * @param {String} url
 * @param {Object} data data参数
 * @param {Boolean} tokenFlag 是否置入token,默认为true
 */
export const DELETE = (url, params = {}, tokenFlag = true) => {
    return instance.delete(url, params);
}

export default request
